<template>
    <div class="notice">
        <div class="title">
            <span>|</span>消息公告
        </div>
        <div class="content">
            <div v-for="(item, index) in noticeList" :key="index" class="a">
                <el-button type="text" style="margin-left: 16px;color:#7F7F7F" @click="handleClick(index)">
                    {{ item.title }}
                </el-button>
                <el-drawer v-model="item.drawer" title="I am the title" :with-header="false" id="notice-drawer">
                    <span>{{ item.title }}</span>
                    <br>
                    <span>{{ item.content }}</span>
                    <br>
                    <el-button type="primary" plain id="queren" @click="btnyes(item.id)">确定</el-button>
                    <br>
                    <span>发布人：{{ item.manager }}</span>
                    <br>
                    <span>发布时间：{{ item.createTime }}</span>
                </el-drawer>
                <span>{{ item.createTime }}</span>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import request from '@/utils/http'

interface UserRoleResponse {
    code: string
    data: {
        roles: string[]
        nickname: string
    }
}
const nickname = ref<string>('')
const res = await request.get('/auth/user/info') as UserRoleResponse;
console.log(res);
nickname.value = res.data.nickname;

interface NoticeItem {
    title: string;
    content: string;
    createTime: string;
    drawer: boolean;
    id: number;
    manager?: string;
}
const noticeList = ref<NoticeItem[]>([])
const getNotice = async () => {
    try {
        const res = await request({
            url: '/jnotice/lish',
            params: {
                size: 999
            }
        })
        console.log(res)
        noticeList.value = res.data.list.map((item: any) => ({
            ...item,
            drawer: false,
        }))
    } catch (error) {
        console.error('获取数据失败:', error)
    }
}
const handleClick = (index: number) => {
    noticeList.value[index].drawer = !noticeList.value[index].drawer;
}
onMounted(() => getNotice())

const btnyes = async (noticeId: number) => {
    try {
        const res = await request.post('/jnotice/lish/setNoticeName', {
            noticeId: Number(noticeId),
            username: nickname.value
        })
        console.log(res)
        // 关闭当前抽屉
        const currentNoticeIndex = noticeList.value.findIndex(item => item.id === noticeId)
        if (currentNoticeIndex !== -1) {
            noticeList.value[currentNoticeIndex].drawer = false
        }
    } catch (error) {
        console.error('设置通知名称失败:', error)
    }
}
</script>

<style scoped>
.notice {
    width: 350px;
    height: 885px;
    background-color: #fff;
    box-shadow: 0px 4px 16px rgba(0, 0, 0, 0.1);
    border: 1px solid #e5e5e5;

    .title {
        font-size: 18px;
        padding: 15px;
        color: #006DB8;

        span {
            font-weight: bold;
            color: #006DB8;
            font-size: 23px;
        }
    }
}

.content {
    span {
        color: #7F7F7F;
        font-size: 12px;
        margin-right: 10px;
    }

    a {
        color: #333;
        font-size: 13px;
        margin-left: 25px;
    }
}

.a {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

#notice-drawer {
    color: #7F7F7F;
}

#queren {
    position: absolute;
    bottom: 0;
    left: 0;
}

#notice-drawer span:first-of-type {
    font-size: 30px;
    font-weight: bold;
    color: #000000;
    font-family: "楷体";

}

#notice-drawer span:nth-of-type(2) {
    font-size: 25px;
    color: #000000;
    padding-top: 20px;
    font-family: "楷体";
    position: absolute;
    top: 60px;
}

#notice-drawer span:nth-of-type(3) {
    font-size: 18px;
    padding-top: 15px;
    position: absolute;
    right: 0;
    bottom: 40px;
    font-family: "楷体";
}

#notice-drawer span:nth-of-type(4) {
    font-size: 18px;
    padding-top: 15px;
    position: absolute;
    right: 10px;
    bottom: 10px;
    font-family: "楷体";
}
</style>
